﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using Lhn.Ds.Core.Controll;

namespace Lhn.Ds.Core
{
    public class MainWindowModel : BaseModel
    {
        private bool _isLogin;

        public bool IsLogin
        {
            get => _isLogin;
            set
            {
                if (_isLogin == value) return;
                _isLogin = value;
                OnPropertyChanged();
            }
        }


        private ObservableCollection<Item> _items = new ObservableCollection<Item>();

        public ObservableCollection<Item> Items
        {
            get => _items;
            set
            {
                if (_items == value) return;
                _items = value;
                OnPropertyChanged();
            }
        }

        private Item _current;

        public Item Current
        {
            get => _current;
            set
            {
                if (_current == value) return;
                var ctl = _current as IControl;
                ctl?.OnSuspended(new ControllEvent() {FromUrl = value?.Uri});
                _current = value;
                (value as IControl)?.OnActive(new ControllEvent());
                OnPropertyChanged();
            }
        }

        public ICommand ClickCommand => new ReplayCommand(arg => { Current = (Item) arg; });

        public MainWindowModel()
        {
            DsContext.InstanceData.SubscribeAction("//UserLogin", this);
            var lockObj = new object();
            BindingOperations.EnableCollectionSynchronization(Items, lockObj);
        }

        public override void Init(object param)
        {
            base.Init(param);
            Current = new Item() {Content = new Login()};
        }

        public override void OnAction(string type, object param)
        {
            switch (type)
            {
                case "//UserLogin":
                    DispatchUI(() =>
                    {
                        {
                            foreach (var item in new[]
                            {
                                new Item()
                                {
                                    Content = new Controll.ActivateBankCard(),
                                    Name = "开卡",
                                    Uri = "/ActivateBankCard"
                                },
                                new Item() {Content = new CancleCard(), Name = "销卡", Uri = "/CancleCard"},
                                new Item()
                                {
                                    Content = new Controll.QueryCardInfo(),
                                    Name = "查询",
                                    Uri = "/QueryCardInfo"
                                },
                                /*  new Item() {Content = new RechargeCard(), Name = "充值", Uri = "/RechargeCard"},*/
                            })
                            {
                                Items.Add(item);
                            }

                            IsLogin = true;
                            Current = Items.FirstOrDefault();
                        }
                    });
                    break;
                case "//UserLogout":
                    Current = new Item() {Content = new Login()};
                    IsLogin = false;
                    break;
            }
        }
    }


    public class Item : INotifyPropertyChanged
    {
        private string _uri;

        public string Uri
        {
            get => _uri;
            set
            {
                if (_uri == value) return;
                _uri = value;
                OnPropertyChanged();
            }
        }

        private object _content;

        public object Content
        {
            get => _content;
            set
            {
                if (_content == value) return;
                _content = value;
                OnPropertyChanged();
            }
        }

        private string _name;

        public string Name
        {
            get => _name;
            set
            {
                if (_name == value) return;
                _name = value;
                OnPropertyChanged();
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}